#include "liblut/core/LutModel.h"

#include <iomanip>

LutModel::LutModel(eLutColorSpace colorspace) :
        m_colorSpace(colorspace)
{
    m_interpolationType=BoundInterpolator::linear;
    m_interpolator=BoundInterpolator(BoundInterpolator::linear);
}

bool
LutModel::addBound(double val, int channel1, int channel2, int channel3)
{
    LutBound newBound(val,channel1,channel2,channel3,m_colorSpace);
    m_lutBounds.insert(newBound);
}

void
LutModel::removeBound(int index)
{
    std::set<LutBound>::iterator it_index=m_lutBounds.begin();
    for(int i=0; i<index; i++)
         {
           if( it_index== m_lutBounds.end()) return;
           it_index++;
         }
    if( it_index== m_lutBounds.end()) return;
    m_lutBounds.erase(it_index);
}

void
LutModel::removeBound(double val)
{
    LutBound boundtest=LutBound(val);
    std::set<LutBound>::iterator it_index,ite;
    it_index=m_lutBounds.find(boundtest);
    if( it_index== m_lutBounds.end()) return;

    m_lutBounds.erase(it_index);
    // need to be clean using find_if and a predicate on LutBound for equal value
    /*for( ite=m_lutBounds.begin() ; ite !=m_lutBounds.end(); ite++)
    {
        if( ite->GetValue() == val )
        {
            it_index=ite;
        }
    }
    if( it_index!=m_lutBounds.end() )
    {
        m_lutBounds.erase(it_index);
    }*/

}

void
LutModel::setInterpolatorType(InterpolatorType interpolationType) {
    m_interpolationType=interpolationType;
    m_interpolator=BoundInterpolator(m_interpolationType);
}

LutModel::InterpolatorType
LutModel::getInterpolatorType() const {
    return m_interpolationType;
}

BoundInterpolator
LutModel::getInterpolator() const {
    return m_interpolator;
}

void
LutModel::clear()
{
    m_lutBounds.clear();
}


ostream& operator<< (ostream &out, LutModel &lut)
{
    // Since operator<< is a friend of the LutModel class, we can access
    // LutModel's members directly.
    out << " colorspace : "<<lut.m_colorSpace<<std::endl;
    out << " bound : "<<std::endl;
    for(LutModel::iterator ite=lut.begin(); ite!=lut.end() ; ite++)
    {
        LutColor c = ite->getColor();
        out<<"\t "<<"value="<<ite->getValue()<<"\t color={"<<setw(3)<<c.getC1();
        out<<","<<setw(3)<<c.getC2()<<","<<setw(3)<<c.getC3()<<"}"<<std::endl;
    }
    return out;
}

